package com.rzj.book.dao;

import com.rzj.book.bo.UserBO;
import com.rzj.book.en.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @ClassName IUserDao
 * @Description TODO
 * @Author Administrator
 * @Date 2020/5/14 20:54
 * @Version 1.0
 */
@Mapper
public interface IUserDao {

    @Select("select * from user")
    List<User> findAll() throws Exception;

    @Select("<script>" +
            "select * from user where 1=1 " +
            "<if test = 'userBO != null' >" +
            "<if test='userBO.name != null'>" +
            " and name like CONCAT('%',#{userBO.name},'%') " +
            "</if>" +
            "<if test='userBO.userName != null'>" +
            " and userName like CONCAT('%',#{userBO.userName},'%') " +
            "</if>" +
            "<if test='userBO.address != null'>" +
            " and address like CONCAT('%',#{userBO.address},'%') " +
            "</if>" +
            "<if test='userBO.tel != null'>" +
            " and tel like CONCAT('%',#{userBO.tel},'%') " +
            "</if>" +
            "<if test='userBO.sex != null'>" +
            " and sex = #{userBO.sex} " +
            "</if>" +
            "<if test='userBO.startDate != null and userBO.endDate'>" +
            " and bir &gt;= #{userBO.startDate} and bir &lt;=#{userBO.endDate} " +
            "</if>" +
            "</if>" +
            " order by bir desc " +
            "<if test='page != null and pageSize != null'>" +
            " LIMIT #{page},#{pageSize} " +
            "</if>" +
            "</script>")
    List<User> findByPage(@Param("userBO") UserBO userBO, @Param("page") Integer page, @Param("pageSize") Integer pageSize);

    @Select("<script>" +
            "select count(*) from user where 1=1 " +
            "<if test = 'userBO != null' >" +
            "<if test='userBO.name != null'>" +
            " and name like CONCAT('%',#{userBO.name},'%') " +
            "</if>" +
            "<if test='userBO.userName != null'>" +
            " and userName like CONCAT('%',#{userBO.userName},'%') " +
            "</if>" +
            "<if test='userBO.address != null'>" +
            " and address like CONCAT('%',#{userBO.address},'%') " +
            "</if>" +
            "<if test='userBO.tel != null'>" +
            " and tel like CONCAT('%',#{userBO.tel},'%') " +
            "</if>" +
            "<if test='userBO.sex != null'>" +
            " and sex = #{userBO.sex} " +
            "</if>" +
            "<if test='userBO.startDate != null and userBO.endDate'>" +
            " and bir &gt;= #{userBO.startDate} and bir &lt;=#{userBO.endDate} " +
            "</if>" +
            "</if>" +
            "</script>")
    Integer findByPageCount(@Param("userBO") UserBO userBO, Integer page, Integer pageSize);

    @Select("select * from user where userId = #{id}")
    User getUserById(@Param("id") Integer id);

    @Insert("insert into user(name, userName, pwd, bir, tel, email, sex,address) values (#{user.name},#{user.userName},#{user.pwd},#{user.bir, jdbcType=TIMESTAMP},#{user.tel},#{user.email},#{user.sex},#{user.address})")
    void addUser(@Param("user") User user);

    @Update({"update user set userName = #{user.userName},pwd = #{user.pwd}," +
            "name = #{user.name},bir = #{user.bir,jdbcType=TIMESTAMP},tel = #{user.tel}," +
            "email = #{user.email},sex = #{user.sex},address = #{user.address} " +
            "where userId= #{user.userId}"
    })
    void updateUser(@Param("user")User user);

    @Delete("delete from user where userId = #{id}")
    void deleteUser(@Param("id")Integer id);

    @Select("select * from user where userName = #{userName}")
    User findByUsername(@Param("userName")String userName);

    @Select("select * from user where userName = #{userName} and pwd = #{pwd}")
    User login(@Param("userName")String userName, @Param("pwd")String pwd);
}
